﻿using System.Collections.Generic;
using ApplicationLibrary.DBManager;
using ApplicationLibrary.ExtendedControls;
using ApplicationLibrary.SqlConnector;
using Plumber.Masters.ModuleData;

namespace Plumber.Masters.Forms
{
    public partial class Descriptions : ApplicationLibrary.DBManager.DocumentForm
    {
        private DescriptionType descType;
        private BindableOject<int> descrType = null;

        public Descriptions(DescriptionType type)
            : base("Descriptions")
        {
            InitializeComponent();
            keyControl = txtTitle;
            descType = type;
        }

        #region Virtual Method

        protected override IRadarParameters GetKeyFromDocument()
        {
            return new RadarDescriptionsParam(descType, txtTitle.Text);
        }

        protected override void OnAttachData()
        {
            dbManager = new dbmanagerDescriptions("titleForm", new RadarDescriptions(descType), this);
            dbManager.AddMaster(this, PL_Descriptions.Name, true);

            descrType = new BindableOject<int>();
        }

        protected override void OnBindData()
        {
            BindObject(descrType, PL_Descriptions.Type);
            BindControl(txtTitle, PL_Descriptions.Code);
            BindControl(txtDescription, PL_Descriptions.Description);
        }

        protected override bool OnAfterAddNew()
        {
            descrType.Value = (int)descType;
            return base.OnAfterAddNew();
        }

        protected override void FocusOnNew()
        {
            txtTitle.Focus();
        }

        #endregion
    }

    #region DbManager dbmanagerDescriptions

    /// <summary>
    /// Classe derivata per gestioni particolari
    /// </summary>
    internal class dbmanagerDescriptions : DBManager
    {
        public dbmanagerDescriptions(string name, RadarForm radar, DocumentForm document)
            : base(name, radar, document)
        { }

        protected override void dAdapter_MasterRowUpdating(object sender, SqlABRowUpdatingEventArgs e)
        {
            base.dAdapter_MasterRowUpdating(sender, e);
        }

        protected override string CreateMasterQuery()
        {
            this.Table = new PL_Descriptions();
            QueryBuilder qB = new QueryBuilder();
            qB.AddSelect();
            qB.AddFrom(this.Table);
            qB.AddCompare(PL_Descriptions.Type, "@p1");
            qB.AddCompare(PL_Descriptions.Code, "@p2");

            return qB.Query;
        }

        protected override List<SqlABParameter> CreateMasterParam()
        {
            List<SqlABParameter> PList = new List<SqlABParameter>();

            SqlABParameter sParam1 = new SqlABParameter("@p1", PL_Descriptions.Type);
            sParam1.Value = PL_Descriptions.Type.DefaultValue;
            PList.Add(sParam1);

            SqlABParameter sParam2 = new SqlABParameter("@p2", PL_Descriptions.Code);
            sParam2.Value = PL_Descriptions.Code.DefaultValue;
            PList.Add(sParam2);
            return PList;
        }

        protected override void SetParameters(ApplicationLibrary.ExtendedControls.IRadarParameters key, DBCollection collection)
        {
            collection.Parameter[0].Value = key[0];
            collection.Parameter[1].Value = key[1];
        }
    }

    #endregion
}